# encoding:utf-8
import urllib, urllib2, sys
import ssl
import json

# client_id 为官网获取的AK， client_secret 为官网获取的SK
ak = "8C9syuhouzZL9VB7K1ym6obE"
sk = "NA6OAmGUYX1i8v4pDiXRPFG33kRNo6jT"
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ak + '&client_secret=' + sk
print(host)
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
    content = json.loads(content)
    access_token = content["access_token"]
    # print(access_token)

access_token = '24.24abd22502bcd491b329dbd4cbeb3260.2592000.1530261290.282335-11324076'
url = 'https://aip.baidubce.com/rpc/2.0/solution/v1/unit_utterance?access_token=' + access_token
while 1:
    sentense = raw_input("请输入想说的话：")
    # post_data = "{\"scene_id\":21890,\"query\":\"\",\"session_id\":\" \"}"
    post_data = {
        "scene_id": 21890,
        "query": str(sentense),
        "session_id": None,
    }
    request = urllib2.Request(url, json.dumps(post_data))
    request.add_header('Content-Type', 'application/json charset=UTF-8')
    response = urllib2.urlopen(request)
    content = response.read()
    if (content):
        content = json.loads(content,encoding='UTF_8')
        if content['result']['action_list'][0]['main_exe'] == 'ORDER':
            print("正在为您查询，请稍后。。。")
            info = content['result']['qu_res']['intent_candidates'][0]["slots"]
            matchdict = {"时间": "user_time", "医院": "user_location", "医生": "user_person"}
            print(info)
            time = info[0]['original_word']
            hospital = info[1]['original_word']
            doctor = info[2]['original_word']
            print("正在为您预约，预约成功！")
            # print("正在为您预约" + str(time) + str(hospital) + str(doctor))
        elif content['result']['action_list'][0]['main_exe'] == 'DESTORY':
            print("正在为您查询，请稍后。。。")
            info = content['result']['qu_res']['intent_candidates'][0]["slots"]
            matchdict = {"时间": "user_time", "医院": "user_location", "医生": "user_person"}
            time = info[0]['original_word']
            hospital = info[1]['original_word']
            doctor = info[2]['original_word']

            print("正在为您毁约" + time + hospital + doctor)
        else:
            say = content['result']['action_list'][0]['say']
            if len(say) >= 1:
                print(say)
            else:
                print("正在为您执行！")
